var MaterialColorData = function (data, mode) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (data, mode) {
      self.mode = mode;
      if (mode === 'test') {
        this.initTestData(data);
        return;
      }

      if (mode === 'create') {
        this.initCreateData(data);
      } else {
        this.initOrderData(data);
      }
    },

    initCreateData: function (data) {
      self.remark = data.remark;
      self.lineNo = data.lineNo;

      var busMaterialList = data.busMaterialList;
      var busFieldConfig = MaterialColorData.busFieldConfig;
      for (var i = 0; i < busMaterialList.length; i++) {
        var obj = busMaterialList[i];
        var field = busFieldConfig[obj.materialType];
        if (field !== undefined) {
          var value = obj.materialModel;
          if (['unitPrice', 'orderTotal'].indexOf(field) !== -1) {
            value = Number(value)
          }
          self[field] = value;
        }
      }

      var wasteRateList = data.wasteRateList;
      var orderTotal = self.orderTotal;
      var wasteRateValue = ProduceMaterialDataModel.getWasteRateValue(orderTotal, wasteRateList);
      var materialDataObj = {};
      var bomMaterialList = data.bomMaterialList;
      var feedTotalList = [],
        yltsTotalList = [];
      for (var i = 0; i < bomMaterialList.length; i++) {
        var bomMaterial = bomMaterialList[i];
        var material = new ProduceMaterialDataModel(bomMaterial, 'material', {
          wasteRateValue: wasteRateValue,
          orderTotal: orderTotal
        });
        materialDataObj[material.materialType] = material;
        feedTotalList.push(material.feedTotal);
        yltsTotalList.push(material.yltsTotal);
      }
      var feedTotal = 0;
      if (feedTotalList.length > 0) {
        feedTotal = _.min(feedTotalList);
      }

      var yltsTotal = 0;
      if (yltsTotalList.length > 0) {
        yltsTotal = _.min(yltsTotalList);
      }
      self.feedTotal = feedTotal;
      self.yltsTotal = yltsTotal;
      self.materialDataObj = materialDataObj;

      var glassesList = data.glassesList;
      var difficultRate = data.difficultLevelRate;
      var glassesDataObj = {};
      for (var i = 0; i < glassesList.length; i++) {
        var glasses = glassesList[i];
        var glassesOrderTotal = glasses.orderTotal;
        glassesOrderTotal = Number(glassesOrderTotal);
        var glassesWasteRateValue = ProduceMaterialDataModel.getWasteRateValue(glassesOrderTotal, wasteRateList);
        glasses = new ProduceMaterialDataModel(glasses, 'glasses', {
          wasteRateValue: glassesWasteRateValue,
          orderTotal: glassesOrderTotal,
          difficultRate: difficultRate
        });
        glassesDataObj[glasses.materialType] = glasses;
      }
      self.glassesDataObj = glassesDataObj;

      var deliveryDateList = data.deliveryDateList;
      var deliveryDates = [];
      for (var i = 0; i < deliveryDateList.length; i++) {
        var deliveryDate = deliveryDateList[i];
        var date = deliveryDate.dhDate;
        date = moment(date).format('YYYY-MM-DD');
        var count = deliveryDate.dhTotal;
        count = Number(count);
        deliveryDates.push({
          date: date,
          count: count
        });
      }
      self.deliveryDateList = deliveryDates;
    },

    initOrderData: function (data) {
      self.remark = data.remark;
      self.lineNo = data.lineNo;

      var materialList = data.materialColorList;
      var busFieldConfig = MaterialColorData.busFieldConfig;
      var feedTotalList = [];
      var materialDataObj = {},
        glassesDataObj = {},
        deliveryDateList = [];
      for (var i = 0; i < materialList.length; i++) {
        var material = materialList[i];
        var materialType = material.materialType;
        if (material.materialCode === null) {
          if (materialType.startsWith("deliveryDate_")) {
            var date = material.dhDate;
            var count = material.dhTotal;
            deliveryDateList.push({
              date: moment(date).format('YYYY-MM-DD'),
              count: Number(count)
            });
          } else {
            var field = busFieldConfig[materialType];
            if (field !== undefined) {
              var value = material.materialModel;
              if (value === undefined) {
                continue;
              }
              if (['unitPrice', 'orderTotal'].indexOf(field) !== -1) {
                value = Number(value)
              }
              self[field] = value;
            }
          }
        } else {
          if (material.materialName === '老花镜') {
            material = new ProduceMaterialDataModel(material, 'glasses');
            glassesDataObj[materialType] = material;
          } else {
            material = new ProduceMaterialDataModel(material, 'material');
            materialDataObj[materialType] = material;
            feedTotalList.push(material.feedTotal);
          }
        }
      }
      if (feedTotalList.length > 0) {
        self.feedTotal = _.min(feedTotalList);
      } else {
        self.feedTotal = 0;
      }
      self.materialDataObj = materialDataObj;
      self.glassesDataObj = glassesDataObj;
      self.deliveryDateList = deliveryDateList;
    },

    initTestData: function (data) {
      self.remark = data.remark;
    }
  };

  this.getTableData = function (materialType, field) {
    var materialDataObj = this.materialDataObj;
    var material = materialDataObj[materialType];
    if (material === undefined) {
      return '';
    }

    var value = material[field];
    if (field === 'orderTotal' && value) {
      value = _.round(value);
    }
    return value;
  };

  this.getGlassesOrderTotal = function (materialType) {
    var glassesDataObj = this.glassesDataObj;
    var glasses = glassesDataObj[materialType];
    if (glasses === undefined) {
      return '';
    }
    return glasses.orderTotal;
  };

  this.getDeliveryDateData = function (key) {
    var arr = key.split('_');
    var index = Number(arr[1]);
    var deliveryDateList = this.deliveryDateList;
    var obj = deliveryDateList[index];
    if (obj === undefined) {
      return '';
    }

    var field = arr[2];
    return obj[field];
  };

  this.getAllMaterialTypeList = function () {
    var materialDataObj = this.materialDataObj;
    return _.keys(materialDataObj);
  };

  this.getAllGlassesTypeList = function () {
    var glassesDataObj = this.glassesDataObj;
    return _.keys(glassesDataObj);
  };

  this.getDeliveryDateCount = function () {
    var deliveryDateList = this.deliveryDateList;
    return deliveryDateList.length;
  };

  this.getObject = function (option) {
    var lineNo = this.lineNo;
    var materialList = [];
    var materialDataObj = this.materialDataObj;
    for (var materialType in materialDataObj) {
      var material = materialDataObj[materialType];
      material = material.getObject(option);
      material.lineNo = lineNo;
      materialList.push(material);
    }

    var glassesDataObj = this.glassesDataObj;
    for (var materialType in glassesDataObj) {
      var glasses = glassesDataObj[materialType];
      glasses = glasses.getObject();
      glasses.lineNo = lineNo;
      materialList.push(glasses);
    }

    var deliveryDateList = this.deliveryDateList;
    for (var i = 0; i < deliveryDateList.length; i++) {
      var obj = deliveryDateList[i];
      materialList.push({
        lineNo: lineNo,
        materialType: `deliveryDate_${i}`,
        dhDate: obj.date,
        dhTotal: obj.count,
        materialFactory: 3
      });
    }

    var busFieldConfig = MaterialColorData.busFieldConfig;
    for (var key in busFieldConfig) {
      var field = busFieldConfig[key];
      var value = self[field];
      if (value === undefined) {
        continue;
      }
      materialList.push({
        lineNo: lineNo,
        materialType: key,
        materialModel: value,
        materialFactory: 3
      });
    }

    return materialList;
  };

  this.getSpecialRemarkData = function (newData) {
    if (SysUtil.isEmpty(newData)) {
      return [`${this.lineNo}&all&all`];
    }

    var materialDataObj = this.materialDataObj;
    var newMaterialDataObj = newData.materialDataObj;
    var specialRemarkList = [];
    for (var materialType in materialDataObj) {
      var material = materialDataObj[materialType];
      var newMaterial = newMaterialDataObj[materialType];
      var arr = material.getMaterialSpecialRemark(newMaterial);
      if (arr === null) {
        continue;
      }
      specialRemarkList.push(`${this.lineNo}&${arr.join('&')}`)
    }
    return specialRemarkList;
  };

  this.setCheckUpdate = function (oldData, config) {
    if (oldData === undefined) {
      this.updateStatusObj = true;
      return;
    }

    var updateStatusObj = {};
    var fields = ['lineNo', 'customerColor', 'customerColorDescription', 'orderTotal', 'feedTotal', 'remark'];
    for (var i = 0; i < fields.length; i++) {
      var field = fields[i];
      var value = this[field];
      var oldValue = oldData[field];
      updateStatusObj[field] = value !== oldValue;
    }

    var materialTypeConfig = config.materialTypeConfig;
    var materialDataObj = this.materialDataObj;
    var oldMaterialDataObj = oldData.materialDataObj;
    var materialUpdateObj = {};
    for (var i = 0; i < materialTypeConfig.length; i++) {
      var obj = materialTypeConfig[i];
      var materialType = obj.materialType;
      var material = materialDataObj[materialType];
      var oldMaterial = oldMaterialDataObj[materialType];
      if (material === undefined || oldMaterial === undefined) {
        materialUpdateObj[materialType] = material !== oldMaterial;
      } else {
        materialUpdateObj[materialType] = material.getCheckUpdateObj(oldMaterial);
      }
    }
    updateStatusObj.materialUpdateObj = materialUpdateObj;

    var glassesConfig = config.glassesConfig;
    var glassesDataObj = this.glassesDataObj;
    var oldGlassesDataObj = oldData.glassesDataObj;
    var glassesUpdateObj = {};
    for (var materialType in glassesConfig) {
      var glasses = glassesDataObj[materialType];
      var oldGlasses = oldGlassesDataObj[materialType];
      if (glasses === undefined || oldGlasses === undefined) {
        glassesUpdateObj[materialType] = glasses !== oldGlasses;
      } else {
        glassesUpdateObj[materialType] = glasses.orderTotal !== oldGlasses.orderTotal;
      }
    }
    updateStatusObj.glassesUpdateObj = glassesUpdateObj;

    var deliveryDateList = this.deliveryDateList;
    var oldDeliveryDateList = oldData.deliveryDateList;
    var deliveryDateUpdateObj = {};
    for (var i = 0; i < deliveryDateList.length; i++) {
      var obj = deliveryDateList[i];
      var oldObj = oldDeliveryDateList[i];
      if (obj === undefined || oldObj === undefined) {
        deliveryDateUpdateObj[i] = obj !== oldObj;
      } else {
        deliveryDateUpdateObj[i] = {
          date: obj.date !== oldObj.date,
          count: obj.count !== oldObj.count
        };
      }
    }
    updateStatusObj.deliveryDateUpdateObj = deliveryDateUpdateObj;
    this.updateStatusObj = updateStatusObj;
  };

  this.checkUpdate = function (columnKey) {
    var updateStatusObj = this.updateStatusObj;
    if (updateStatusObj === undefined) {
      return false;
    }

    if (updateStatusObj === true) {
      return true;
    }

    if (columnKey.indexOf('_') === -1) {
      return updateStatusObj[columnKey];
    }

    var arr = columnKey.split('_');
    var type = arr[0];
    if (type === 'material') {
      var materialUpdateObj = updateStatusObj.materialUpdateObj;
      var obj = materialUpdateObj[arr[1]];
      if (obj === true) {
        return true;
      }
      var field = arr[2];
      if (['orderTotal', 'feedTotal', 'yltsTotal', 'remark'].indexOf(field) === -1) {
        return false;
      }
      return obj[field];
    }

    if (type === 'glasses') {
      var glassesUpdateObj = updateStatusObj.glassesUpdateObj;
      return glassesUpdateObj[arr[1]];
    }

    if (type === 'deliveryDate') {
      var deliveryDateUpdateObj = updateStatusObj.deliveryDateUpdateObj;
      var obj = deliveryDateUpdateObj[arr[1]];
      if (obj === true) {
        return true;
      }
      return obj[arr[2]];
    }
    return false;
  };

  fn.initData(data, mode);
};

MaterialColorData.busFieldConfig = {
  "订单数": "orderTotal",
  "投料数": "feedTotal",
  "用料数": "yltsTotal",
  "客户色号": "customerColor",
  "客户颜色描述": "customerColorDescription"
};
